package util;

import java.util.ArrayList;
import java.util.List;

public class TreeNode<E> {

	private E value;
	private TreeNode<E> father;
	private List<TreeNode<E>> listPointers;
	
	public TreeNode(E value, TreeNode<E> father) {
		super();
		this.father = father;
		this.value = value;
		listPointers = new ArrayList<TreeNode<E>>();
	}
	
	public void addPointer(E value, TreeNode<E> node){
		listPointers.add(new TreeNode<E>(value, node));
	}
	
	public void addPointer(TreeNode<E> node){
		listPointers.add(node);
	}
	
	public boolean isLeaf(){
		return listPointers.isEmpty();
	}

	public E getValue() {
		return value;
	}

	public void setValue(E value) {
		this.value = value;
	}

	public List<TreeNode<E>> getListPointers() {
		return listPointers;
	}

	public void setListPointers(List<TreeNode<E>> listPointers) {
		this.listPointers = listPointers;
	}

	public TreeNode<E> getFather() {
		return father;
	}

	public void setFather(TreeNode<E> father) {
		this.father = father;
	}
	
	public TreeNode<E> getPointer(E value, TreeNode<E> found){
		if(found == null){
			if(!isLeaf()){
				for(TreeNode<E> node : listPointers){
					if(node.getValue().equals(value)){
						found = node;
					}else{
						found = node.getPointer(value, found);
					}
				}
			}
		}
		
		return found;
	}

	@Override
	public String toString() {
		String print = value + " <";
		if(!isLeaf()){
			for(TreeNode<E> node : listPointers){
				print += node.toString();
			}
		}
		print += ">";
		return print;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((value == null) ? 0 : value.hashCode());
		return result;
	}

	@SuppressWarnings("unchecked")
	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (!(obj instanceof TreeNode)) {
			return false;
		}
		TreeNode<E> other = (TreeNode<E>) obj;
		if (value == null) {
			if (other.value != null) {
				return false;
			}
		} else if (!value.equals(other.value)) {
			return false;
		}
		return true;
	}
	
}
